﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using PineLake.Drmwe.Lib.ResultMsg;
using PineLake.Drmwe.Lib.EnumText;
using Microsoft.AspNetCore.Mvc.Controllers;
using System.Text;
using PineLake.Drmwe.Encrypts.DES;

namespace RepositoryMultipleDataBases.Filters
{
    /// <summary>
    /// 异步接口操作取消错误信息过滤器
    /// </summary>
    public class OperationCancelledExceptionFilter : ExceptionFilterAttribute
    {
        private readonly ILogger _Logger;

        private readonly IDES _DES;
        public OperationCancelledExceptionFilter(ILoggerFactory loggerFactory,IDES DES)
        {
            _Logger = loggerFactory.CreateLogger<OperationCancelledExceptionFilter>();
            _DES = DES;
        }
        //public override void OnException(ExceptionContext context)
        //{
        //    if (context.Exception is OperationCanceledException)
        //    {
        //        var Action = (ControllerActionDescriptor)context.ActionDescriptor;
        //        HttpContext HttpCon = context.HttpContext;
        //        var Route = HttpCon.Request.Path.Value;
        //        var ClientIp = HttpCon.Connection.RemoteIpAddress.MapToIPv4().ToString();
        //        //兼容 Nginx 只能获取到本地IP的方法
        //        if (HttpCon.Request.Headers.ContainsKey("X-Forwarded-For"))
        //        {
        //            var NginxIp = HttpCon.Request.Headers["X-Forwarded-For"].ToString();
        //            ClientIp = string.IsNullOrEmpty(NginxIp) == true ? ClientIp : NginxIp;
        //        }
        //        var UserAgentString = HttpCon.Request?.Headers["user-agent"].ToString() ?? "Unknown";
        //        _Logger.LogInformation(message: $"Controllers:{Action.ActionConstraints} WebApiRoute: {Route}  Action: {Action.ActionName} 请求被取消! 客户端IP: {ClientIp}");
        //        context.ExceptionHandled = true;
        //        //设置返回内容
        //        //var result = new ApiResultMsg()
        //        //{
        //        //    StatusCode = (int)ApiStatusCodeEnum.RequestWasCancelled,
        //        //    Data = new ActionResultMsg()
        //        //    {
        //        //        ActionCode = (int)ActionStatusCodeEnum.Unsuccessful,
        //        //        Info = "请求被取消!",
        //        //    },
        //        //    Info = ApiStatusCodeEnum.RequestWasCancelled.GetEnumText()
        //        //};
        //        //返回状态码
        //        //context.Result = new JsonResult(result);
        //        context.Result = new StatusCodeResult(400);
        //    }
        //}

        public override Task OnExceptionAsync(ExceptionContext context)
        {
            if (context.Exception is OperationCanceledException)
            {
                var Action = (ControllerActionDescriptor)context.ActionDescriptor;
                HttpContext HttpCon = context.HttpContext;
                var Route = HttpCon.Request.Path.Value;
                var ClientIp = HttpCon.Connection.RemoteIpAddress.MapToIPv4().ToString();
                //兼容 Nginx 只能获取到本地IP的方法
                if (HttpCon.Request.Headers.ContainsKey("X-Forwarded-For"))
                {
                    var NginxIp = HttpCon.Request.Headers["X-Forwarded-For"].ToString();
                    ClientIp = string.IsNullOrEmpty(NginxIp) == true ? ClientIp : NginxIp;
                }
                var UserAgentString = HttpCon.Request?.Headers["user-agent"].ToString() ?? "Unknown";
                _Logger.LogWarning(message: $"ControllerName: {Action.ControllerName} WebApiRoute: {Route} RequestMethod: {HttpCon.Request.Method}  Action: {Action.ActionName} 请求被取消! 客户端IP: {ClientIp}");
                context.ExceptionHandled = true;
                //设置返回内容
                //var result = new ApiResultMsg()
                //{
                //    StatusCode = (int)ApiStatusCodeEnum.RequestWasCancelled,
                //    Data = new ActionResultMsg()
                //    {
                //        ActionCode = (int)ActionStatusCodeEnum.Unsuccessful,
                //        Info = "请求被取消!",
                //    },
                //    Info = ApiStatusCodeEnum.RequestWasCancelled.GetEnumText()
                //};
                //返回状态码
                //context.Result = new JsonResult(result);
                context.Result = new StatusCodeResult(400);
            }
            return base.OnExceptionAsync(context);
        }
    }
}
